package com.css.fxfzaqbz.modules.base.controller;

import com.css.fxfzaqbz.base.response.RestResponse;
import com.css.fxfzaqbz.base.upload.entity.AttachmentInfoEntity;
import com.css.fxfzaqbz.base.upload.service.AttachmentInfoService;
import com.css.fxfzaqbz.common.utils.ZipDownloadUtil;
import com.css.fxfzaqbz.modules.base.entity.NetworkanalystVersion;
import com.css.fxfzaqbz.modules.base.service.HypergraphService;
import com.css.fxfzaqbz.modules.data.entity.AuxiliaryMeasures;
import com.css.fxfzaqbz.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: fxfzaqbz
 * @Author: zyt
 * @CreateDate: 2023/10/24 14:26
 * 超图
 */
@RestController
@RequestMapping("/hypergraph")
@Slf4j
public class HypergraphController {

    @Resource
    HypergraphService hypergraphService;
    @Autowired
    private AttachmentInfoService attachmentInfoService;

    /**
     * 获取超图server口令并根据类型调用超图服务
     * @return
     */
    @PostMapping("/getServerToken")
    public RestResponse getServerToken(@RequestBody Map<String, Object> map){
        RestResponse restResponse=null;
        try {
            restResponse=hypergraphService.getServerToken(map);
        } catch (Exception e) {
            e.printStackTrace();
            restResponse=RestResponse.fail("获取失败！");
        }
        return restResponse;
    }

    /**
     * 安保系统--刷新指定超图工作空间数据源数据集
     * @return
     */
    @GetMapping("/getWorkspaceReload")
    public RestResponse getWorkspaceReload(String dataset){
        RestResponse restResponse=null;
        try {
            boolean bool = hypergraphService.getWorkspaceReload(dataset);
            if(bool){
                restResponse = RestResponse.succeed("刷新成功");
            }else{
                restResponse=RestResponse.fail("刷新失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            restResponse=RestResponse.fail("刷新失败！");
        }
        return restResponse;
    }

    /**
     * 根据起终点经纬度获取交通网络分析服务的最佳路径分析
     * @return
     */
    @PostMapping("/getNetworkanalystPath")
    public RestResponse getNetworkanalystPath(@RequestBody Map<String, Object> map){
        RestResponse restResponse=null;
        try {
            restResponse=hypergraphService.getNetworkanalystPath(map);
            //刷新指定超图工作空间数据源数据集
            boolean bool = hypergraphService.getWorkspaceReload("sys_networkanalyst_path");
        } catch (Exception e) {
            e.printStackTrace();
            restResponse=RestResponse.fail("获取失败！");
        }
        return restResponse;
    }

    /**
     * 工作空间发布交通网络分析服务
     * @return
     */
    @PostMapping("/newWorkspaceNetworkVersion")
    public RestResponse newWorkspaceNetworkVersion(@RequestBody NetworkanalystVersion networkanalystVersion){
        RestResponse restResponse=null;
        try {
            String attachIds = networkanalystVersion.getAttachIds();
            String[] ids = attachIds.split(",");
            for(int i=0;i<ids.length;i++){
                //根据id和文件名获取真实的放在磁盘上的文件
                AttachmentInfoEntity attachmentInfoEntity = attachmentInfoService.getAttach(ids[i]);
                String attachName = attachmentInfoEntity.getAttachName();
                String attachPath = attachmentInfoEntity.getAttachPath();
                String fileDirId = attachmentInfoEntity.getFileDirId();
                fileDirId = fileDirId.replace('\\','/');
                String url = fileDirId + attachPath;
                if(attachName.endsWith(".smwu")){
                    networkanalystVersion.setSmwuUrl(url);
                }else if(attachName.endsWith(".udbx")){
                    networkanalystVersion.setUdbxUrl(url);
                }
            }
            restResponse=hypergraphService.newWorkspaceNetworkVersion(networkanalystVersion);
        } catch (Exception e) {
            e.printStackTrace();
            restResponse=RestResponse.fail("发布失败！");
        }
        return restResponse;
    }

    /**
     * 工作空间发布地图服务
     * @return
     */
    @PostMapping("/newWorkspaceRestMap")
    public RestResponse newWorkspaceRestMap(@RequestBody NetworkanalystVersion networkanalystVersion){
        RestResponse restResponse=null;
        try {
            String attachIds = networkanalystVersion.getAttachIds();
            String[] ids = attachIds.split(",");
            for(int i=0;i<ids.length;i++){
                //根据id和文件名获取真实的放在磁盘上的文件
                AttachmentInfoEntity attachmentInfoEntity = attachmentInfoService.getAttach(ids[i]);
                String attachName = attachmentInfoEntity.getAttachName();
                String attachPath = attachmentInfoEntity.getAttachPath();
                String fileDirId = attachmentInfoEntity.getFileDirId();
                fileDirId = fileDirId.replace('\\','/');
                String url = fileDirId + attachPath;
                if(attachName.endsWith(".smwu")){
                    networkanalystVersion.setSmwuUrl(url);
                }else if(attachName.endsWith(".udbx")){
                    networkanalystVersion.setUdbxUrl(url);
                }
            }
            restResponse=hypergraphService.newWorkspaceRestMap(networkanalystVersion);
        } catch (Exception e) {
            e.printStackTrace();
            restResponse=RestResponse.fail("发布失败！");
        }
        return restResponse;
    }

    /**
     * 工作空间发布地图服务
     * @return
     */
    @PostMapping("/deleteWorkspaceByVersion")
    public RestResponse deleteWorkspaceByVersion(String dataVersion){
        RestResponse restResponse=null;
        try {
            boolean bool = hypergraphService.deleteWorkspaceByVersion(dataVersion);
            if(bool){
                restResponse = RestResponse.succeed("删除成功！");
            }else{
                restResponse=RestResponse.fail("删除失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            restResponse=RestResponse.fail("删除失败！");
        }
        return restResponse;
    }

    /**
     * 按国家级、省级查询交通网络分析服务版本列表
     * @return
     */
    @GetMapping("/getNetworkVersionList")
    public RestResponse getNetworkVersionList(HttpServletRequest request, NetworkanalystVersion networkanalystVersion){
        RestResponse restResponse=null;
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            networkanalystVersion.setCurPage(curPage);
            networkanalystVersion.setPageSize(pageSize*2);
            restResponse=hypergraphService.getNetworkVersionList(networkanalystVersion);
        } catch (Exception e) {
            e.printStackTrace();
            restResponse=RestResponse.fail("查询失败！");
        }
        return restResponse;
    }

    /**
     * 分页查询交通网络分析服务版本列表
     * @return
     */
    @GetMapping("/getNetworkVersionListPage")
    public RestResponse getNetworkVersionListPage(HttpServletRequest request, NetworkanalystVersion networkanalystVersion){
        RestResponse restResponse=null;
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            networkanalystVersion.setCurPage(curPage);
            networkanalystVersion.setPageSize(pageSize*2);
            restResponse=hypergraphService.getNetworkVersionListPage(curPage, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            restResponse=RestResponse.fail("查询失败！");
        }
        return restResponse;
    }

    /**
     * 批量逻辑删除交通网络分析服务版本列表
     * @return
     */
    @PostMapping("/deleteNetworkanalystVersion")
    public RestResponse deleteNetworkanalystVersion(@RequestBody Map<String, Object> map){
        RestResponse restResponse=null;
        try {
            String idsStr = (String) map.get("idsStr");
            restResponse=hypergraphService.deleteNetworkanalystVersion(idsStr);
        } catch (Exception e) {
            e.printStackTrace();
            restResponse=RestResponse.fail("查询失败！");
        }
        return restResponse;
    }

    /**
     * 批量下载超图路网工作空间文件
     * @return
     */
    @PostMapping("/downloadNetworkFileZip")
    public RestResponse downloadNetworkFileZip(HttpServletResponse response, String id) {
        RestResponse restResponse=null;
        try{
            NetworkanalystVersion networkanalystVersion = hypergraphService.getNetworkVersionById(id);
            String smwuUrl = networkanalystVersion.getSmwuUrl();
            String udbxUrl = networkanalystVersion.getUdbxUrl();
            String tempFileName = "超图工作空间数据编号" + networkanalystVersion.getVersion();
            int lastIndex = smwuUrl.lastIndexOf("/");
            String newUploadPath = smwuUrl.substring(0, lastIndex+1);
            String smwuName = smwuUrl.substring(lastIndex+1);
            String udbxName = udbxUrl.substring(lastIndex+1);
            //获取指定路径下的文件
            File file = new File(smwuUrl);
            File file1 = new File(udbxUrl);
            List<File> _files = new ArrayList<>();
            _files.add(file);
            _files.add(file1);
            List<File> zipFiles = _files;
            /*//获取指定路径下的所有文件名
            File file = new File(newUploadPath);
            File[] files = file.listFiles();
            if (files == null) {
                return RestResponse.fail("下载失败，该目录下不存在文件");
            }
            String _temp = "";
            List<File> _files = new ArrayList<>();
            for (int i = 0; i < files.length; i++) {
                _temp = files[i].getName();
                if (_temp.indexOf(smwuName) != -1){
                    _files.add(files[i]);
                }else if (_temp.indexOf(udbxName) != -1){
                    _files.add(files[i]);
                }
            }
            List<File> zipFiles = _files;*/
            StringBuffer sb = new StringBuffer();
            if (!CollectionUtils.isEmpty(zipFiles)) {
                String zipName = tempFileName + ".zip";
                String msg = ZipDownloadUtil.zipDownload(response, newUploadPath, zipName, zipFiles);
                sb.append(msg);
            } else {
            /*sb.append("没有可下载文件");
            return RestResponse.succeed(sb);*/
                String zipName = tempFileName + ".zip";
                String msg = ZipDownloadUtil.zipDownload(response, newUploadPath, zipName, new ArrayList<File>());
                sb.append(msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            restResponse=RestResponse.fail("查询失败！");
        }
        return restResponse;
    }

    /**
     * 工作空间导入路网数据
     * @return
     */
    /*@PostMapping("/importWorkspaceNetworkData")
    public RestResponse importWorkspaceNetworkData(@RequestBody Map<String, Object> map){
        RestResponse restResponse=null;
        try {
            restResponse=hypergraphService.importWorkspaceNetworkData(map);
        } catch (Exception e) {
            e.printStackTrace();
            restResponse=RestResponse.fail("导入失败！");
        }
        return restResponse;
    }*/

    /**
     * 路网服务--刷新指定超图工作空间数据源数据集
     * @return
     */
    /*@GetMapping("/getNetworkWorkspaceReload")
    public RestResponse getNetworkWorkspaceReload(){
        RestResponse restResponse=null;
        try {
            boolean bool = hypergraphService.getNetworkWorkspaceReload();
            if(bool){
                restResponse = RestResponse.succeed("刷新成功");
            }else{
                restResponse=RestResponse.fail("刷新失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            restResponse=RestResponse.fail("刷新失败！");
        }
        return restResponse;
    }*/
}
